package com.hzncc.flowable_diboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.binding.Binder;
import com.diboot.core.vo.LabelValue;
import com.hzncc.flowable_diboot.vo.BasicSalesOrgListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import com.diboot.core.util.S;
import com.diboot.core.service.impl.BaseServiceImpl;

import com.hzncc.flowable_diboot.entity.BasicSalesOrg;
import com.hzncc.flowable_diboot.mapper.BasicSalesOrgMapper;
import com.hzncc.flowable_diboot.service.BasicSalesOrgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * 销售组织基础档案 相关Service实现类
 *
 * @author MyName
 * @version 1.0
 * @date 2024-05-30
 * Copyright © MyCorp
 */
@Slf4j
@Service
public class BasicSalesOrgServiceImpl extends BaseServiceImpl<BasicSalesOrgMapper, BasicSalesOrg> implements BasicSalesOrgService {

    @Override
    public List<LabelValue> getLabelList(String salesGroup, String distributeChannel, String productionGroup) {
        QueryWrapper<BasicSalesOrg> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(BasicSalesOrg::getSalesDept)
                .eq(S.isNotBlank(salesGroup), BasicSalesOrg::getSalesGroup, salesGroup)
                .eq(S.isNotBlank(distributeChannel), BasicSalesOrg::getDistributeChannel, distributeChannel)
                .eq(S.isNotBlank(productionGroup), BasicSalesOrg::getProductionGroup, productionGroup);
        List<BasicSalesOrg> entityList = getEntityList(queryWrapper);
        List<BasicSalesOrgListVO> basicSalesOrgListVOS = Binder.convertAndBindRelations(entityList, BasicSalesOrgListVO.class);
        return basicSalesOrgListVOS.stream().map(BasicSalesOrgListVO::getSalesDeptLabel).toList();
    }

    @Override
    public List<LabelValue> getDisLabelList(String salesGroup) {
        QueryWrapper<BasicSalesOrg> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(BasicSalesOrg::getDistributeChannel)
                .eq(S.isNotBlank(salesGroup), BasicSalesOrg::getSalesGroup, salesGroup);
        List<BasicSalesOrg> entityList = getEntityList(queryWrapper);
        Set<String> exist = new HashSet<>();
        entityList = entityList.stream().filter(e -> {
            if (exist.contains(e.getDistributeChannel())) {
                return false;
            } else {
                exist.add(e.getDistributeChannel());
                return true;
            }
        }).toList();
        List<BasicSalesOrgListVO> basicSalesOrgListVOS = Binder.convertAndBindRelations(entityList, BasicSalesOrgListVO.class);
        return basicSalesOrgListVOS.stream().map(BasicSalesOrgListVO::getDistributeChannelLabel).toList();
    }

    @Override
    public List<LabelValue> getProLabelList(String salesGroup) {
        QueryWrapper<BasicSalesOrg> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(BasicSalesOrg::getProductionGroup)
                .eq(S.isNotBlank(salesGroup), BasicSalesOrg::getSalesGroup, salesGroup);
        List<BasicSalesOrg> entityList = getEntityList(queryWrapper);
        Set<String> exist = new HashSet<>();
        entityList = entityList.stream().filter(e -> {
            if (exist.contains(e.getProductionGroup())) {
                return false;
            } else {
                exist.add(e.getProductionGroup());
                return true;
            }
        }).toList();
        List<BasicSalesOrgListVO> basicSalesOrgListVOS = Binder.convertAndBindRelations(entityList, BasicSalesOrgListVO.class);
        return basicSalesOrgListVOS.stream().map(BasicSalesOrgListVO::getProductionGroupLabel).toList();
    }
}